using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [ToolboxData("<{0}:RadioButtonField runat=server></{0}:RadioButtonField>")]
    public class RadioButtonField : DataControlField
    {
        protected override DataControlField CreateField()
        {
            return new RadioButtonField();
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            if (cellType == DataControlCellType.DataCell)
            {
                RadioButton btn = new RadioButton();
                btn.Enabled = false;
                if ((rowState & DataControlRowState.Edit) != 0 ||
                    (rowState & DataControlRowState.Insert) != 0)
                {
                    btn.Enabled = true;
                }

                cell.Controls.Add(btn);
                btn.DataBinding += new EventHandler(btn_DataBinding);
            }
            else if(cellType == DataControlCellType.Header && !string.IsNullOrEmpty(HeaderText))
            {
                cell.Text = HeaderText;
            }
            else if (cellType == DataControlCellType.Footer && !string.IsNullOrEmpty(FooterText))
            {
                cell.Text = FooterText;
            }
        }

        void btn_DataBinding(object sender, EventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            IDataItemContainer container = null;
            Control current = btn;
            while (container == null && current.NamingContainer != null)
            {
                current = current.NamingContainer;
                container = current as IDataItemContainer;
            }

            object val = DataBinder.Eval(container.DataItem, DataField);
            if (val is bool)
            {
                btn.Checked = (bool)val;
            }
            else if (val is string)
            {
                if (val != null && ((string)val).ToLower() == "true")
                {
                    btn.Checked = true;
                }
                else
                {
                    btn.Checked = false;
                }
            }
        }

        private string dataField;
        public string DataField
        {
            get { return dataField; }
            set { dataField = value; }
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if ((rowState & DataControlRowState.Edit) != 0 ||
                (rowState & DataControlRowState.Insert) != 0)
            {
                RadioButton btn = cell.Controls[0] as RadioButton;
                if(btn != null)
                {
                    dictionary.Add(this.DataField, btn.Checked);
                }
            }
        }
    }
}
